//
//  KeyBoardView.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/29.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

class EmojiKeyBoardView: UIView {
    
    lazy var allEmojis = EmojiKeyboardViewModel.shareEmojiViewModel.allEmojis
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
       
        
        
        //添加子控件
        addSubview(emojiBottomView)
        addSubview(emojiView)
        
        
        //添加控件的约束
        emojiBottomView.snp_makeConstraints { (make) -> Void in
            make.leading.bottom.trailing.equalTo(self)
            make.height.equalTo(37)
        }
        emojiView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            make.bottom.equalTo(emojiBottomView.snp_top)
        }
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let layout = emojiView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: KscreenWidth, height: self.frame.height - emojiBottomView.frame.height)
    }

    
    
    //MARK: - 懒加载控件
    //底部view
    private lazy var emojiBottomView:EmojiBottomView = {
       let view = EmojiBottomView(frame: CGRectZero)
        view.buttonClickCloseSure = { (type) -> () in
            //TODO: 点击buttom中的按钮时执行
        }
        return view
    }()
    
    //表情view
    private lazy var emojiView:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing = 0
        
       let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        
        view.dataSource = self
        view.pagingEnabled = true
        view.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
        return view
    }()
    
}

extension EmojiKeyBoardView:UICollectionViewDataSource{
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return self.allEmojis.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (self.allEmojis[section].count - 1) / 20 + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.cyanColor()
        return cell
        
    }
    

}
